//
// Created by RandBII on 2020/12/25.
//

#ifndef FFMPEGFORANDROID_AUDIODECODER_H
#define FFMPEGFORANDROID_AUDIODECODER_H

extern "C" {
#include <libavutil/samplefmt.h>
#include <libswresample/swresample.h>
#include <libavutil/opt.h>
#include <libavutil/audio_fifo.h>
}

#include "render/audio/AudioRender.h"
#include "Decoder.h"
#include "../DecoderBase.h"

static const int AUDIO_DST_SAMPLE_RATE = 44100;

static const int AUDIO_DST_CHANNEL_COUNTS = 2;
//音频编码通道格式( DSD =  direct stream digital
// DST=以上ISO DFF DSF文件的压缩编码，使用这种编码技术可以减少文件尺寸，但播放的时候需要实时解压，对CPU要求比较高 )
static const uint64_t AUDIO_DST_CHANNEL_LAYOUT = AV_CH_LAYOUT_STEREO;

//音频编码比特率
static const int AUDIO_DST_BIT_RATE = 64000;

static const int ACC_SAMPLE_RATE = 1024;


class AudioDecoder : public DecoderBase {

public:
    AudioDecoder(char *url);

    virtual ~AudioDecoder();

    void SetAudioRender(AudioRender *render);

    static long GetAudioDecoderTimestampForAVSync(void *context);

    float GetCurrentPosition();


private:
    virtual void OnDecoderReady();

    virtual void OnDecoderDone();

    virtual void OnFrameAvailable(AVFrame *frame);

    virtual void ClearCache();

    const AVSampleFormat DST_SAMPLE_FORMAT = AV_SAMPLE_FMT_S16;

    AudioRender *m_AudioRender = nullptr;

    SwrContext *m_SwrContext = nullptr;

    uint8_t *m_AudioOutBuffer = nullptr;

    int m_nbSample = 0;
    int m_DstFrameSize = 0;

};


#endif //FFMPEGFORANDROID_AUDIODECODER_H
